package com.niuyubo.jsontojava;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


public class JsonToJavaBean {
    public static void main(String[] args) {
        JsonToJavaBean bean = new JsonToJavaBean();
//        System.out.println(JSON.toJSONString(bean));
        String beanToJson = bean.beanToJson();
        System.out.println(beanToJson);

        TestBean2 bean1 = bean.jsonToBean(beanToJson);
        System.out.println(bean1);
//        System.out.println(bean1.toString());

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("1");
        String[] array = bean.CollectionToArray(list);
        System.out.println(array);
        System.out.println(Arrays.toString(array));

        List<String> list1 = bean.ArrayToCollection(array);
        System.out.println(list1);


//        //数组和列表输出
//        String[] array1 = new String[10];
//        List<String> list4 = new ArrayList<>(5);
//        for(String str : array1) {
//            System.out.println(str + "");
//        }
//        System.out.println(list4);
    }

    private String beanToJson() {
        TestBean1 bean = new TestBean1();
        bean.setName("Tom");
        bean.setAddress("shanghai");
        bean.setAge("9");
        List<String> hobby = new ArrayList<>();
        hobby.add("football");
        hobby.add("piano");
//        hobby = Arrays.asList("football","piano");
        bean.setHobby(hobby);
        return JSON.toJSONString(bean);
    }

    private TestBean2 jsonToBean(String json) {
        return JSONObject.parseObject(json, TestBean2.class);
    }


    private String[] CollectionToArray(List<String> list) {
        int arrlen = list.size();
        String[] resArr = new String[arrlen];
//        System.out.println(Arrays.toString(resArr));
        return list.toArray(resArr);
    }

    private List<String> ArrayToCollection(String[] array) {
        return Arrays.stream(array).collect(Collectors.toList());
    }



}
